#ifndef _BUFFERMANAGER_H_
#define _BUFFERMANAGER_H_

#include "block.h"
#include <string>
#include <iostream>
#define BLOCK_NUM 512
#define FILE_NOT_FOUNT_ERROR (-0xff)
class BufferManager {
public:
	BufferManager();
	~BufferManager();
	//fileoffset is the block number in the file
	Block readBlock(std::string filename, int fileoffset) throw (...);
	bool deleteFile(std::string);
	bool writeBlock(std::string filename, int fileoffset, Block block);
	bool clearBlock(std::string filename, int fileoffset);
	bool writeThroughBlock(std::string filename, int fileoffset, Block block);
	bool writeThroughAllBlocks();
	void showDetails()
	{
		std::cout << "after read/write block " << head->block.offset << ": the buffer's order is:";
		pointer = head;
		while (pointer)
		{
			std::cout << pointer->block.offset << " ";
			pointer = pointer->next;
		}
		std::cout << std::endl;
	}
private:
	bool writethroughblock(const Block &block);
	bool readblock(std::string filename, int fileoffset, Block &block);
	struct list{
		Block block;
		list *next;
		list()
		{
			next = NULL;
		}
	}*head, *tail, *pointer, *last;
	int block_num;
	bool isfull;
};

#endif